<?php

/*
	Graphing functions for PNG based graphs
*/

function getGraphImageURL($x,$y,$title,$type="linear",$width=NULL,$height=NULL) {
	
	array_multisort($x,$y);
	$y = urlencode(json_encode($y));
	$x = urlencode(json_encode($x));
	$title = urlencode($title);
	$append=(isset($width)&&isset($height))?"&width={$width}&height={$height}":"";
	
	$url=getAppBaseURL()."graph/?yaxis={$y}&xaxis={$x}&title={$title}&type={$type}{$append}";
		
	return $url;
	
} 

function getGraphImageURL1($ln1,$ln2,$ln3,$ln4,$xaxis,$title,$width=NULL,$height=NULL) {

	/*
	$ln1 = urlencode(json_encode($ln1));
	$ln2 = urlencode(json_encode($ln2));
	$ln3 = urlencode(json_encode($ln3));
	$ln4 = urlencode(json_encode($ln4));
	*/
	//$x = urlencode(json_encode($xaxis));
	$title = urlencode($title);
	
	// Writing into file.
	// Remove the hardcoding later
	$filename = md5("En-View_".rand(0,1000)).".txt";
	$fp = fopen("/home/indrajit/public_html/v1/graphdata/".$filename,"w+");
	$data = ["ln1"=>$ln1,"ln2"=>$ln2,"ln3"=>$ln3,"ln4"=>$ln4,"xaxis"=>$xaxis];
	fwrite($fp,json_encode($data));
	fclose($fp);
	//-------------------
	$append=(isset($width)&&isset($height))?"&width={$width}&height={$height}":"";
	// ln1={$ln1}&ln2={$ln2}&ln3={$ln3}&ln4={$ln4}
	$url=getAppBaseURL()."graph/?file={$filename}&title={$title}&type=multiline{$append}";
		
	return $url;
	
} 

function getGrahpAsImage($yaxis,$xaxis,$title,$width=500,$height=300){

	ini_set('error_reporting','E_ALL & ~E_STRICT');
	include("jpgraph/src/jpgraph.php");
	include("jpgraph/src/jpgraph_line.php");
    //$xaxis=(int)$xaxis;
	// Setup the graph
	$graph = new Graph($width,$height);
	$graph->SetScale("textlin");
	
	$graph->title->Set($title);
	$graph->SetBox(false);
	
	// Y-axis styling 
	$graph->yaxis->HideZeroLabel();
	$graph->yaxis->HideLine(false);
	$graph->yaxis->HideTicks(false,false);
	$graph->ygrid->SetFill(false);
	

	// X-axis styling 
	$graph->xaxis->SetTickLabels($xaxis);

	// Background Image
	//$graph->SetBackgroundImage("tiger_bkg.png",BGIMG_FILLFRAME);

	// Draw the Plot 
	$p1 = new LinePlot($yaxis);
	$graph->Add($p1);
	$p1->SetColor("#55bbdd");
	//$p1->SetLegend('Line 1');
	$p1->mark->SetType(MARK_FILLEDCIRCLE,'',1.0);
	$p1->mark->SetColor('#55bbdd');
	$p1->mark->SetFillColor('#55bbdd');
	$p1->SetCenter();
	$graph->legend->SetFrameWeight(1);
	$graph->legend->SetColor('#4E4E4E','#00A78A');
	$graph->legend->SetMarkAbsSize(8);

	// Output line
	$graph->Stroke();
}

function getPieGrahpAsImage($data,$legend,$title,$width=300,$height=200){
	ini_set('error_reporting','E_ALL & ~E_STRICT');

	require_once ('jpgraph/src/jpgraph.php');
	require_once ('jpgraph/src/jpgraph_pie.php');
		$tot=0;
	foreach($data as $d){
		$tot+=$d;
	}
	if($tot==0)
		return;
	$graph = new PieGraph($width,$height);
	 
	$graph->title->Set($title);
	$graph->title->SetFont(FF_FONT1,FS_BOLD);
	 
	$p1 = new PiePlot($data);
	$p1->SetLegends($legend);
	$p1->SetCenter(0.4);
	$p1->ShowBorder(false,true);
	$p1->SetColor('black');
	 
	$graph->Add($p1);
	$graph->Stroke();
}

function getMultilineGraphAsImage($ln1,$ln2,$ln3,$ln4,$xaxis,$title,$width=500,$height=300){
	ini_set('error_reporting','E_ALL & ~E_STRICT');

	include("jpgraph/src/jpgraph.php");
	include("jpgraph/src/jpgraph_line.php");
	include("jpgraph/src/jpgraph_date.php");
	
	$datay1 = json_decode($ln1);
	$datay2 = json_decode($ln2);
	$datay3 = json_decode($ln3);
	$datay4 = json_decode($ln4);
	
	$graph = new Graph(700,300);
	$graph->SetScale("textlin");

	//$theme_class= new UniversalTheme;
	//$graph->SetTheme($theme_class);

	$graph->title->Set($title);
	$graph->SetBox(false);

	$graph->yaxis->HideZeroLabel();
	$graph->yaxis->HideLine(false);
	$graph->yaxis->HideTicks(false,false);

	$graph->SetScale( 'datlin' );
	$graph->xaxis->SetTickLabels($xaxis);
	$graph->ygrid->SetFill(false);
	//$graph->SetBackgroundImage("../images/tiger_bkg.png",BGIMG_FILLFRAME);

	$p1 = new LinePlot($datay1);
	$graph->Add($p1);

	$p2 = new LinePlot($datay2);
	$graph->Add($p2);

	$p3 = new LinePlot($datay3);
	$graph->Add($p3);

	$p4 = new LinePlot($datay4);
	$graph->Add($p4);

	$p1->SetColor("#55bbdd");
	$p1->SetLegend('High');
	$p1->SetWeight(2);
	
	//$p1->mark->SetType(MARK_FILLEDCIRCLE,'',1.0);
	$p1->mark->SetColor('#55bbdd');
	$p1->mark->SetFillColor('#55bbdd');
	$p1->SetCenter();

	$p2->SetColor("#F407D0");
	$p2->SetLegend('Low');
	$p1->SetWeight(2);
	//$p2->mark->SetType(MARK_UTRIANGLE,'',1.0);
	$p2->mark->SetColor('#F407D0');
	$p2->mark->SetFillColor('#F407D0');
	$p2->value->SetMargin(14);
	$p2->SetCenter();

	$p3->SetColor("#eee");
	$p3->SetLegend('Mean');
	$p1->SetWeight(2);
	//$p3->mark->SetType(MARK_UTRIANGLE,'',1.0);
	$p3->mark->SetColor('#eee');
	$p3->mark->SetFillColor('#eee');
	$p3->value->SetMargin(14);
	$p3->SetCenter();

	$label="";
	$line4 =explode(' ',$title); 
	 for($i=2;$i<count($line4);$i++){
		if(isset($line4[$i])){
			$label.=$line4[$i]." ";
		}
		else{
		 exit;
		}
	 }
	$p4->SetColor("#ggg");
	$p4->SetLegend($label);
	$p1->SetWeight(2);
	$p4->mark->SetType(MARK_FILLEDCIRCLE,'',2);
	$p4->mark->SetSize(2);
	$p4->mark->SetColor('#ggg');
	$p4->mark->SetFillColor('#ggg');
	//$p4->value->SetMargin(14);
	$p4->SetCenter();

	$graph->legend->SetFrameWeight(1);
	$graph->legend->SetColor('#4E4E4E','#00A78A');
	$graph->legend->SetMarkAbsSize(8);


	// Output line
	$graph->Stroke();

}
?>